/**
 * @author Steve Cox
 * @date 10/19/2011
 * @description controller for the Equipment page of the Thoratec
 * Digital Hub project. This page lists the equipment for a specified patient
 * or location.
*/
public with sharing class MM_EquipmentSummaryController {
   /**
    * Constructor - initilize the page with default filters, page size,
    * and populate lists.
    */
   public MM_EquipmentSummaryController() {
      allChecked = false;
      show = 'all';
      limitTo = 'loc';
      todoFilter = 'thisMonth';
      sortField = 'Name__c';
      equipPageSize = 20;

      this.go();
      this.refreshToDos();
      
      //TODO what to do if there are more records than we can display?
      System.assert(equipmentController.getCompleteResult());
   }


   //--------------------------------------------------------------------------
   // Equipment
   public List<EquipmentWrapper> equipment { get; private set; }
   private String previousSortField { get; set; }
   public String sortField { get; set; }

   // the 'Show' filter
   public String show { get; set; }
   public List<SelectOption> showOptions {
      get {
         if (null == showOptions) {
            showOptions = new List<SelectOption> {
               new SelectOption('all', 'All Equipment')
            };
         }
         return showOptions;
      }
      private set;
   }

   // the 'limit to' filter
   public String limitTo { get; set; }
   public List<SelectOption> limitToOptions {
      get {
         if (null == limitToOptions) {
            limitToOptions = new List<SelectOption> {
               new SelectOption('loc', 'Location / Patient')
            };
         }
         return limitToOptions;
      }
      private set;
   }

   // additional filters
   public String snLot { get; set; }
   public Boolean openServiceRequestsOnly { get; set; }

   /**
    * Check or uncheck all equipment
    */
   public Boolean allChecked { get; set; }
   public void checkAll() {
      for (EquipmentWrapper e : equipment) {
         e.isSelected = allChecked;
      }
   }

   /**
    * apply the equipment search criteria and display the first page of results
    */
   public void go() {
      fetchEquipment(sortField, 'asc');
   }

   // the standard set controller is used to display equipment with pagination
   private ApexPages.StandardSetController equipmentController;

   /**
    * Select the list of equipment based on specified sort and order params, as
    * well as filter selections
    */
   private void fetchEquipment(String sortParam, String orderParam) {
      //TODO: apply filter criteria
      equipmentController = new ApexPages.StandardSetController(Database.query(
         'SELECT Name, Name__c, Serial_Lot_Number__c FROM Equipment__c ORDER BY ' + 
         sortParam + ' ' + orderParam));
      equipmentController.setPageSize(equipPageSize);
      equipmentController.setPageNumber(equipPage);
      this.setEquipmentList();
   }

   /**
    * A helper class for wrapping equipment-related data
    */
   public class EquipmentWrapper {
      public Boolean isSelected { get; set; }
      public Equipment__c equipment { get; set; }
      public EquipmentWrapper(Equipment__c e, Boolean selected) {
         isSelected = selected;
         equipment = e;
      }
   }

   /**
    * Get the paginated list of equipment as a list of EquipmentWrappers
    */
   private void setEquipmentList() {
      equipment = new List<EquipmentWrapper>();
      for (SObject e : equipmentController.getRecords()) {
         equipment.add(new EquipmentWrapper((Equipment__c)e, false));
      }
   }

   /**
    * Sort the equipment list according to the sort field. If this is called
    * twice in a row for the same sort field, a descending sort will be applied
    */
   public void sortEquipment() {
      String order = (previousSortField == sortField) ? 'desc' : 'asc';
      previousSortField = (previousSortField == sortField) ? null : sortField;
      fetchEquipment(sortField, order);
   }

   // pagination
   public Integer equipPage { get { return equipmentController.getPageNumber(); }}
   public Integer equipPageSize { get; set; }
   public Integer equipCount { get { return equipmentController.getResultSize(); }}
   public Boolean equipHasNext { get { return equipmentController.getHasNext(); }}
   public Boolean equipHasPrev { get { return equipmentController.getHasPrevious(); }}
   public String equipPageOf {
      get {
         final Integer first = (equipPage - 1) * equipPageSize + 1;
         return String.format(Label.MM_PageNtoNofN, new String[] {
            String.valueOf(first),
            String.valueOf(first - 1 + equipment.size()),
            String.valueOf(equipCount)});
      }
   }

   public void equipFirst() { equipmentController.first(); setEquipmentList();}
   public void equipLast() { equipmentController.last(); setEquipmentList();}
   public void equipPrev() { equipmentController.previous(); setEquipmentList();}
   public void equipNext() { equipmentController.next(); setEquipmentList();}
   

   //--------------------------------------------------------------------------
   // ToDos
   public List<Task> todos { get; private set; }
   public Task todo { get; set; }
   
   // filter
   public String todoFilter { get; set; }
   public List<SelectOption> todoFilterOptions { get; private set; }
   {
      todoFilterOptions = new List<SelectOption> {
        new SelectOption('thisMonth', 'This Month')
      };
   }

   /**
    * Re-query the list of ToDos
    */
   public void refreshToDos() {
      todos = [SELECT Subject FROM Task];
   }

   public void saveToDo() {
   }

   public void todoSetContext() {
      ID id = ApexPages.CurrentPage().getParameters().get('id');
      todo = [SELECT Subject FROM Task WHERE ID = :id];
   }


   //--------------------------------------------------------------------------
   // Inventory
}